<?php

header("Content-type: image/png");

require_once("database.inc.php");
require_once("functions.inc.php");
require_once("track.inc.php");

if (!isset($_GET['nolog'])) hit();

if (!is_numeric($_GET['c']) || !is_numeric($_GET['l'])) die;

$new = 0;
if (isset($_GET['n']) && $_GET['n'] == "1") $new = 1;

$histo = 0;
if (isset($_GET['h']) && $_GET['h'] == "1") $histo = 1;

$date = getLastUpdate();

if (preg_match("/^\\d{4}-\\d{2}-\\d{2}$/", $_GET['date']) || preg_match("/^new$/", $_GET['date'])) {$date = $_GET['date'];}

$leagueid = $_GET['c'];
$level = $_GET['l'];

$serienames = array();
$dbh = connect_db("dbm");

$seriestable = "series";
if ($new == 1) {
	$seriestable = "series_new";
} else {
	$seriestable = "series_old";
}

$query = mysql_query("SELECT * FROM $seriestable WHERE CountryID = $leagueid AND level = $level ORDER BY level ASC, seriesid ASC", $dbh);
while ($row = mysql_fetch_array($query)) {
	$serienames[$row['SeriesID']] = $row['SeriesName'];
}

$condition = "userid != 0";

if (isset($_GET['sup']) && $_GET['sup'] == 1) {
	$condition = "hassupporter = 1";
}

$querytext = "SELECT seriesid, count(*) num FROM `teams_$date` WHERE $condition AND leagueid = $leagueid GROUP BY seriesid";

if (isset($_GET['act']) && $_GET['act'] == 1) {
	$querytext = "SELECT seriesid, 8 - ROUND(AVG(UNIX_TIMESTAMP( FetchedDate ) - UNIX_TIMESTAMP( LastLoginDate )) / (86400*7)) num FROM `teams_$date` WHERE $condition AND leagueid = $leagueid GROUP BY seriesid";
}

if (isset($_GET['age']) && $_GET['age'] == 1) {
	$subquery = mysql_query("SELECT UNIX_TIMESTAMP(MAX(signupdate)) oldest FROM `teams_$date` WHERE leagueid = $leagueid");
	while($row = mysql_fetch_array($subquery)) {
		$oldest = $row['oldest'];	
	}
	$query = "SELECT seriesid, (something with $oldest) num FROM `teams_$date` WHERE $condition AND leagueid = $leagueid GROUP BY seriesid";
}

$query = mysql_query("$querytext", $dbh);
while ($row = mysql_fetch_array($query)) {
	$wos[$row['seriesid']] = $row['num'];
}

if ($_GET['b'] > 0) {
	$blocksize = $_GET['b'];
} else {
	$blocksize = 8;	
}

$vertical_multiplier = 1;
$fillgrid = 0;

if ($level < 6) {
	$bpl = pow(2,$level)/2;
} else {
	if ($new == 1) {
		if ($level <= 7) {
			$bpl = 32;
		} else if ($level <= 9) {
			$bpl = 32;
#			$fillgrid = 1;
			$vertical_multiplier = 0.5;
		} else if ($level <= 11) {
			$bpl = 64;
#			$fillgrid = 1;
		} else if ($level <= 13) {
			$bpl = 64;
#			$fillgrid = 1;
			$vertical_multiplier = 0.5;
		} else {
			die ("no system defined for 14-level leagues and bigger...\n");
		}
	} else {
		$bpl = 32;
	}
}

//if $histo is 1, add margins and space for the histogram
if ($histo == 1) 
	$im = imagecreatetruecolor(2*$bpl*$blocksize, $bpl*$blocksize);
else
	$im = imagecreatetruecolor($bpl*$blocksize, $bpl*$blocksize);

$bg		= imagecolorallocate($im,127,127,127);

$white		= imagecolorallocate($im,255,255,255);
$grey		= imagecolorallocate($im,232,232,232);
$pink		= imagecolorallocate($im,255,0  ,255);
$red		= imagecolorallocate($im,255,0  ,0  );
$orange		= imagecolorallocate($im,255,144,0  );
$yellow		= imagecolorallocate($im,255,255,0  );
$cyan		= imagecolorallocate($im,0  ,192,192);
$green		= imagecolorallocate($im,0  ,160,0  );
$blue		= imagecolorallocate($im,0  ,0  ,192);
$black		= imagecolorallocate($im,0  ,0  ,0  );

if (isset($_GET['yb']) && $_GET['yb'] == 1) {

	$bg	= imagecolorallocate($im,255,255,255);
	
	$grey	= imagecolorallocate($im,255,255,0  );
	$pink	= imagecolorallocate($im,0  ,0  ,255);
	$red	= imagecolorallocate($im,191,191,0);
	$orange	= imagecolorallocate($im,191,191,191);
	$yellow	= imagecolorallocate($im,0  ,0  ,191);
	$cyan	= imagecolorallocate($im,127,127,0  );
	$green	= imagecolorallocate($im,127,127,127);
	$blue	= imagecolorallocate($im,0  ,0  ,127);
}

imagefill($im,0,0,$bg);

$px = 0;
$py = 0;


$bnum = 0;
foreach(array_keys($serienames) as $seriesid) {
	$bnum++;
	if(isset($wos[$seriesid])) {
		switch ($wos[$seriesid]) {
			case 8: imagefilledrectangle($im, $px, $py, $fillgrid+$px+$blocksize-2, $fillgrid+$py+($vertical_multiplier*$blocksize)-2, $black); break;
			case 7: imagefilledrectangle($im, $px, $py, $fillgrid+$px+$blocksize-2, $fillgrid+$py+($vertical_multiplier*$blocksize)-2, $blue); break;
			case 6: imagefilledrectangle($im, $px, $py, $fillgrid+$px+$blocksize-2, $fillgrid+$py+($vertical_multiplier*$blocksize)-2, $green); break;
			case 5: imagefilledrectangle($im, $px, $py, $fillgrid+$px+$blocksize-2, $fillgrid+$py+($vertical_multiplier*$blocksize)-2, $cyan); break;
			case 4: imagefilledrectangle($im, $px, $py, $fillgrid+$px+$blocksize-2, $fillgrid+$py+($vertical_multiplier*$blocksize)-2, $yellow); break;
			case 3: imagefilledrectangle($im, $px, $py, $fillgrid+$px+$blocksize-2, $fillgrid+$py+($vertical_multiplier*$blocksize)-2, $orange); break;
			case 2: imagefilledrectangle($im, $px, $py, $fillgrid+$px+$blocksize-2, $fillgrid+$py+($vertical_multiplier*$blocksize)-2, $red); break;
			case 1: imagefilledrectangle($im, $px, $py, $fillgrid+$px+$blocksize-2, $fillgrid+$py+($vertical_multiplier*$blocksize)-2, $pink); break;
			default: imagefilledrectangle($im, $px, $py, $fillgrid+$px+$blocksize-2, $fillgrid+$py+($vertical_multiplier*$blocksize)-2, $grey);
		}
	} else {
		imagefilledrectangle($im, $px, $py, $fillgrid+$px+$blocksize-2, $fillgrid+$py+($vertical_multiplier*$blocksize)-2, $grey);
	}
	if ($blocksize >= 32) {
		imagecenteredstring($im, 1, $px, $px+$blocksize-2, $py+0.5*$vertical_multiplier*$blocksize-3, utf8_decode($serienames[$seriesid]), $white);
	}
	if ($bnum==$bpl) {
		$bnum = 0;
		$px = 0;
		$py += $vertical_multiplier*$blocksize;
	} else {
		$px += $blocksize;
	}
}

if ($histo == 1) {
	imagefilledrectangle($im,$bpl*$blocksize,0,2*$bpl*$blocksize - 1,2*$bpl*$blocksize - 1,$white);
	imageline($im,$bpl*$blocksize + 10, 0, $bpl*$blocksize + 10, $bpl*$blocksize - 1, $black);
}

imagepng($im);
imagedestroy($im);

function imagecenteredstring ( &$img, $font, $xMin, $xMax, $y, $str, $col ) {
	$textWidth = imagefontwidth( $font ) * strlen( $str );
	$xLoc = ( $xMax - $xMin - $textWidth ) / 2 + $xMin + $font;
	imagestring( $img, $font, $xLoc, $y, $str, $col );
}
?> 
